import Vue from 'vue'
import App from './App.vue'
import router from './index'
import './plugins/element.js'
import axios from 'axios'
import './assets/css/global.css'
import SessionManager from './utils/sessionManager.js'

// 配置axios
// axios.defaults.baseURL = 'http://localhost:4041'  // 注释或删除这行，因为已在vue.config.js中配置了代理
Vue.prototype.$http = axios  // 将axios实例挂载到Vue原型上
Vue.config.productionTip = true

// 添加请求拦截器，统一添加授权头
axios.interceptors.request.use(
  config => {
    const token = window.sessionStorage.getItem('token')
    if (token) {
      // 添加Bearer前缀，符合OAuth 2.0规范
      config.headers['Authorization'] = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 创建全局SessionManager实例
Vue.prototype.$sessionManager = SessionManager

new Vue({
  router,
  sessionManager: SessionManager, // 添加到根实例选项中
  render: h => h(App)
}).$mount('#app')
